#include "Projectile.h"

GLuint GetProjectileDrawList(float size = 0.0)
{
	static GLuint list = 0;
	
	if(!list)
	{
		list = glGenLists(1);
		glNewList(list, GL_COMPILE);
			glColor3f(.4,.4,.4);
			glutSolidCube(size);
		glEndList();
	}

	return list;
}

CProjectile::CProjectile(void)
: m_bActive(false)
{
	float size = 1;
	SetCorners( vec3(size/2), -vec3(size/2) );

	GetProjectileDrawList(size);
}

CProjectile::~CProjectile(void)
{
}

// Update the status of this projectile
void CProjectile::Update(float delta)
{
	if (m_bActive == false) return;

	// Update TimeLife of projectile. Set to inactive if too long

	// Check for collision. if yes, then set to inactive

	// Check for out of bounds. if yes, then set to inactive
	if( wPos.distancesquared(origin) > MAX_DIST_SQR )
	{
		wPos = dir = origin = vec3(0.0f); // reset everything
		m_bActive = false;
	}

	// Update Position
	wPos += dir * delta;
}

void CProjectile::Draw(void)
{
	if (m_bActive == false) return;
	glRotatef( atan(dir.x / dir.z)*180.0f/3.142f , 0, 1, 0);
	glCallList( GetProjectileDrawList() );
}

// Activate the projectile. true == active, false == inactive
void CProjectile::SetActivate(bool m_bActivate)
{
	m_bActive = (m_bActivate) ? true : false;
}

// get status of the projectile. true == active, false == inactive
bool CProjectile::GetStatus(void)
{
	return m_bActive;
}